
export const CUSTOM_COVER_POPUP = "customPopup"

/**
 * 坐标矫正公共方法
 * @param {*} lng 
 * @param {*} lat 
 * @returns 
 */
export const transPoint = (lng,lat) => {
    var x = lng, y = lat;
    var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * Math.PI);
    var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * Math.PI);
    var bdLng = z * Math.cos(theta) + 0.0065;
    var bdLat = z * Math.sin(theta) + 0.006;

    return new BMap.Point(bdLng,bdLat)
}

export const markerWarningLabel = (point,title) => {

    var label = new BMap.Label(title, {
        position : point,
        offset   : new BMap.Size(-110, 60)
    });

    label.setStyle({
        color : "#FFECEC",
        fontSize : "28px",
        background:"linear-gradient(180deg, #3E1B1B 0%, #2D0E0E 100%)",
        boxShadow:'inset 0px 0px 14px 0px #693A3A',
        height : "64px",
        width:'220px',
        borderRadius: '10px',
        textAlign:'center',
        border:'0',
        lineHeight : "64px",
        fontFamily:"微软雅黑"
    });
    
    return label;
}

/**
 * 计算角度
 * @param {*} n 
 * @param {*} next 
 * @returns 
 */
export const computeAngle = (n,next) => {
	let ret
	let w1 = n.lat/180 * Math.PI
	let j1 = n.lng/180 * Math.PI
	let w2 = next.lat/180 * Math.PI
	let j2 = next.lng/180 * Math.PI
	ret = 4 * Math.pow(Math.sin((w1 - w2) / 2), 2) - Math.pow(Math.sin((j1 - j2) / 2) * (Math.cos(w1) - Math.cos(w2)), 2);
	ret = Math.sqrt(ret);
	let temp = Math.sin((j1 - j2) / 2) * (Math.cos(w1) + Math.cos(w2));
	ret = ret/temp;
	ret = Math.atan(ret) / Math.PI * 180 ;
	ret += 90;
	if(j1 - j2 < 0) {
		if (w1 - w2 < 0) {
			ret;
		} else {
			ret = -ret + 180;
		}
	} else {
		if (w1 - w2 < 0) {
			ret = 180 + ret;
		} else {
			ret = -ret;
		}
	}
	return ret ;
}

/**
 * 根据距离计算线之间所有的点位
 * @param {*} distance 
 */
export const computesLineAllMarker = (ax,ay,bx,by,d,distance = 1000) => {
    ax = parseFloat(ax)
    ay = parseFloat(ay)
    bx = parseFloat(bx)
    by = parseFloat(by)
    d =  parseFloat(d)
    
    var n =  Math.ceil(d/distance); 
    var dx = distance*(bx - ax)/d;
    var dy = distance*(by-ay)/d;
    var xc = dx + ax;
    var yc = dy + ay;
    var points = []

    for(var i=1;i<=n;i++){
        var xc = i*dx + ax;
        var yc = i*dy + ay;
        points.push(transPoint(xc,yc))
    }
    return points
}

/**
 * 转换覆盖物类型
 * @param {*} key 
 */
export const convertCoverType = (key) => {

    const darwingType = [
        {key:0,val:BMAP_DRAWING_POLYLINE},
        {key:1,val:BMAP_DRAWING_MARKER},
        {key:2,val:BMAP_DRAWING_CIRCLE},
        {key:3,val:BMAP_DRAWING_POLYGON},
        {key:4,val:BMAP_DRAWING_RECTANGLE},
        {key:5,val:CUSTOM_COVER_POPUP},
    ]

    let index = darwingType.findIndex(it => it.key == key)

    return darwingType[index].val
}


/**
 * 创建自定义覆盖物
 * @param {*} point 点位 
 * @param {*} html html
 * @returns 
 */
export const createCustomPopup = (point,html,xSkew=50,ySkew=200) => {

    const ComplexCustomOverlay = function(point){
        this._point = point;
    }
  
    ComplexCustomOverlay.prototype = new BMap.Overlay();
  
    ComplexCustomOverlay.prototype.initialize = function(map){
        this._map = map;
        
        map.getPanes().labelPane.appendChild(html);
        return html;
    }
  
    ComplexCustomOverlay.prototype.draw = function(){
        var map = this._map;
        var pixel = map.pointToOverlayPixel(this._point);
        if(this.V == null) return
        this.V.style.left = pixel.x + xSkew + "px";
        this.V.style.top = pixel.y - ySkew + "px";
    }
  
            
    return new ComplexCustomOverlay(point);
}



/**
 * 计算面的中心点
 * @param {*} lnglatarr 
 * @returns 
 */
export const  calculateCenter = (lnglatarr) => {
    var total = lnglatarr.length;
    var X = 0, Y = 0, Z = 0;

    lnglatarr.forEach( item => {
        var lng = item.lng * Math.PI / 180;
        var lat = item.lat * Math.PI / 180;
        var x, y, z;
        x = Math.cos(lat) * Math.cos(lng);
        y = Math.cos(lat) * Math.sin(lng);
        z = Math.sin(lat);
        X += x;
        Y += y;
        Z += z;
    })
    
    X = X / total;
    Y = Y / total;
    Z = Z / total;
    
    var Lng = Math.atan2(Y, X);
    var Hyp = Math.sqrt(X * X + Y * Y);
    var Lat = Math.atan2(Z, Hyp);
    return new BMap.Point(Lng * 180 / Math.PI, Lat * 180 / Math.PI);
}




export const wgs84ToGcj02ToBd09Public = (lng, lat, ...args) => {
    const xPI = (3.14159265358979324 * 3000.0) / 180.0;
    const PI = 3.1415926535897932384626;
    const a = 6378245.0;
    const ee = 0.00669342162296594323;
    // WGS84转GCj02
    let dlat = transformlat(lng - 105.0, lat - 35.0);
    let dlng = transformlng(lng - 105.0, lat - 35.0);
    let radlat = (lat / 180.0) * PI;
    let magic = Math.sin(radlat);
    magic = 1 - ee * magic * magic;
    let sqrtmagic = Math.sqrt(magic);
    dlat = (dlat * 180.0) / (((a * (1 - ee)) / (magic * sqrtmagic)) * PI);
    dlng = (dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * PI);
    let mglat = lat + dlat;
    let mglng = lng + dlng;
    // 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
    let z =
      Math.sqrt(mglng * mglng + mglat * mglat) + 0.00002 * Math.sin(mglat * xPI);
    let theta = Math.atan2(mglat, mglng) + 0.000003 * Math.cos(mglng * xPI);
    let bdlng = z * Math.cos(theta) + 0.0065;
    let bdlat = z * Math.sin(theta) + 0.006;
    // return [bdlng, bdlat]
   
    /*  Object.assign({}, target, source)，其中第一个参数是一个空对象，第二个参数是目标对象，第三个参数是源对象。该方法会将源对象中不存在于目标对象中的属性复制到目标对象中，而不会覆盖目标对象中已经存在的同名属性。 */
    return Object.assign({}, {longitude: bdlng, latitude: bdlat}, ...args);
  }



   
function transformlat(lng, lat) {
    const PI = 3.1415926535897932384626;
    let ret =
      -100.0 +
      2.0 * lng +
      3.0 * lat +
      0.2 * lat * lat +
      0.1 * lng * lat +
      0.2 * Math.sqrt(Math.abs(lng));
    ret +=
      ((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) *
        2.0) /
      3.0;
    ret +=
      ((20.0 * Math.sin(lat * PI) + 40.0 * Math.sin((lat / 3.0) * PI)) * 2.0) /
      3.0;
    ret +=
      ((160.0 * Math.sin((lat / 12.0) * PI) + 320 * Math.sin((lat * PI) / 30.0)) *
        2.0) /
      3.0;
    return ret;
  }
   
function transformlng(lng, lat) {
const PI = 3.1415926535897932384626;
let ret =
    300.0 +
    lng +
    2.0 * lat +
    0.1 * lng * lng +
    0.1 * lng * lat +
    0.1 * Math.sqrt(Math.abs(lng));
ret +=
    ((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) *
    2.0) /
    3.0;
ret +=
    ((20.0 * Math.sin(lng * PI) + 40.0 * Math.sin((lng / 3.0) * PI)) * 2.0) /
    3.0;
ret +=
    ((150.0 * Math.sin((lng / 12.0) * PI) +
    300.0 * Math.sin((lng / 30.0) * PI)) *
    2.0) /
    3.0;
return ret;
}